import { reactive, computed, toRefs, ref } from 'vue';
import { getMerchantPageData } from '@/api/merchant';
import { Pagination, PaginationQuery } from '@/types/table';
import { TableState, CheckAble } from '@/types/table';

const tableQuery: PaginationQuery = reactive({
    page: 0,
    size: 9,
    sort: 'id,desc',
});
const countryRegionList = ref<any>([]);
// 角色信息
const state: TableState & CheckAble = reactive({
    dataSource: [],
    selectedRow: {},
    selectedRowKeys: [],
    visible: false,
    loading: false,
    pagination: {
        total: 0,
        current: computed(() => tableQuery.page + 1),
    },
});

// 获取table数据
const getMerchantPageDataData = async (tableQuery: PaginationQuery) => {
    state.loading = true;
    const { content, totalElements } = await getMerchantPageData(tableQuery);
    state.loading = false;
    state.dataSource = content;
    state.pagination.total = totalElements;
};

// 页码改变
const handleTableChange = (pagination: Pagination) => {
    const { current } = pagination;
    tableQuery.page = current - 1;
    getMerchantPageDataData(tableQuery);
};

const handleEdit = (user: { [x: string]: any }) => {
    if (localStorage.Region) {
        // console.log(localStorage.Region);
        countryRegionList.value = JSON.parse(localStorage.Region);
    }
    state.selectedRow = user;
    state.visible = true;
};

// 取消弹窗
const handleCancle = () => {
    state.visible = false;
};
// 新增
const handleAddProd = () => {
    if (localStorage.Region) {
        // console.log(localStorage.Region);
        countryRegionList.value = JSON.parse(localStorage.Region);
    }
    state.selectedRow = {};
    state.visible = true;
};
// 批量导入
const handleImport = () => {
    console.log('批量导入');
};

export default {
    tableQuery,
    countryRegionList,
    handleTableChange,
    getMerchantPageDataData,
    ...toRefs(state),
    handleCancle,
    handleAddProd,
    handleEdit,
    handleImport,
};
